package com.legstar.talend.xsd2xml;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.legstar.xsd.InvalidParameterException;
import com.legstar.xsd.InvalidXsdException;

/**
 * COBOL to TOS schema translation using file system.
 * <p/>
 * An extension of the core API that deals with reading/ writing to the file
 * system.
 * 
 */
public class Cob2TosSchemaIO extends Cob2TosSchema {

    /** Execution parameters. */
    private Cob2TosSchemaModel _model;

    /** Logger. */
    private final Log _log = LogFactory.getLog(getClass());

    public Cob2TosSchemaIO(final Cob2TosSchemaModel model) {
        super();
        _model = model;
    }

    /**
     * Generates a new TOS schema.
     * 
     * @throws IOException if basic read/write operation fails
     * @throws InvalidXsdException if XML schema read is invalid
     * @throws InvalidParameterException if one of the parameters is invalid
     */
    @SuppressWarnings("unchecked")
    public void execute() throws IOException, InvalidXsdException,
            InvalidParameterException {

        _log.info("COBOL to TOS schema translator started");
        checkParameters();

        File inputCobXsd = getModel().getInputCobXsd();

        if (inputCobXsd.isDirectory()) {
            Collection<File> xsdFiles = FileUtils.listFiles(inputCobXsd,
                    new String[] { "xsd" }, true);
            for (File file : xsdFiles) {
                execute(file);
            }
        } else {
            execute(inputCobXsd);
        }

        _log.info("COBOL to TOS schema translator ended");
    }

    /**
     * Generates a new TOS schema.
     * 
     * @param file the input COBOL-annotated XML schema
     * 
     * @throws IOException if basic read/write operation fails
     * @throws InvalidXsdException if XML schema read is invalid
     */
    protected void execute(final File file) throws InvalidXsdException,
            IOException {
        if (_log.isDebugEnabled()) {
            _log.debug("Processing File " + file);
        }
        translate(FileUtils.readFileToString(file, "UTF-8"), getModel()
                .getTargetTosFolder(),
                FilenameUtils.getBaseName(file.getName()));

    }

    /**
     * Sanity checks this model.
     * 
     * @throws InvalidParameterException if a parameter is invalid
     */
    public void checkParameters() throws InvalidParameterException {

        if (getModel().getInputCobXsd() == null) {
            throw new InvalidParameterException(
                    "No input COBOL-annotated XML schema file or folder specified");
        }

        if (getModel().getTargetTosFolder() == null) {
            throw new InvalidParameterException(
                    "No target folder or file was specified for TOS schemas");
        }

    }

    /**
     * @return the execution parameters
     */
    public Cob2TosSchemaModel getModel() {
        return _model;
    }

}